package com.bw.zg3park2212.park

import android.util.Log
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bw.base.entity.ParkTypeEntity
import com.bw.base.entity.ParkTypeEntityItem
import com.bw.base.net.ApiService
import com.bw.base.net.Res

//type做什么类型接口查询
class ParkDataSource(val apiService: ApiService,val para:String,val type:String):PagingSource<Int,ParkTypeEntityItem>() {
    override fun getRefreshKey(state: PagingState<Int, ParkTypeEntityItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ParkTypeEntityItem> {
        try {
            //当前页码
            val page=params.key?:1
            var res:Res<ParkTypeEntity>?=null
            if (type=="类型"){
                res=apiService.getParkType(para)
            }else if (type=="状态"){
                res=apiService.getZTPark(para)
            }else if(type=="区域"){
                res=apiService.getAddresPark(para.toInt())
            }else if(type=="车牌号"){
                res=apiService.getCodePark(para)
            }

          //  val prePage=if (page!=1) page-1 else null
            val nextPage=if (res?.data?.size!=0) page+1 else null
            return LoadResult.Page(res!!.data.sortedByDescending { it.id },null,null)
        }catch (e:Exception){
            return LoadResult.Error(e)
        }
    }
}